﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;
using Telerik.Web.UI;
using System.Data.SqlClient;

public partial class Admin_UserControls_AssignRole : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void adminRadListBox_PreRender(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = Admin.GetAll();
            adminRadListBox.DataTextField = "email";
            adminRadListBox.DataValueField = "admin_id";
            adminRadListBox.DataSource = dt;
            adminRadListBox.DataBind();
        }
    }

    protected void roleRadListBox2_PreRender(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitListBoxRole(0, "");
        }
    }

    protected void assignRoleRadListBox_PreRender(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        assignRoleRadListBox.Items.Clear();
        InitAssignRoleList(0, "");
        //}
    }

    protected void InitListBoxRole(int ParentID, string Tab)
    {
        DataTable dt = Role.GetRoleByParent(ParentID);
        foreach (DataRow row in dt.Rows)
        {
            string text = Tab + row["role_name"].ToString();
            string value = row["role_id"].ToString();
            RadListBoxItem item = new RadListBoxItem(text, value);
            roleRadListBox2.Items.Add(item);
            InitListBoxRole(Convert.ToInt32(value), Tab + "...........");
        }
    }

    protected void InitAssignRoleList(int ParentID, string Tab)
    {
        if (adminRadListBox.SelectedIndex < 0)
        {
            RadListBoxItem item = new RadListBoxItem("Chưa chọn admin", "0");
            assignRoleRadListBox.Items.Add(item);
            return;
        }

        int AdminID = Convert.ToInt32(adminRadListBox.SelectedValue);
        DataTable dtAR = AdminRole.GetByAdminID(AdminID);

        DataTable dt = Role.GetRoleByParent(ParentID);
        foreach (DataRow row in dt.Rows)
        {
            string text = Tab + row["role_name"].ToString();
            string value = row["role_id"].ToString();
            RadListBoxItem item = new RadListBoxItem(text, value);
            foreach (DataRow r in dtAR.Rows)
            {
                if (r["role_id"].ToString() == value)
                    item.Selected = true;
            }
            assignRoleRadListBox.Items.Add(item);
            InitAssignRoleList(Convert.ToInt32(value), Tab + "...........");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (adminRadListBox.SelectedIndex < 0)
        {
            resultLabel.Text = "(*) Chọn admin trước khi phân quyền";
            return;
        }

        int AdminID = Convert.ToInt32(adminRadListBox.SelectedValue);
        DataTable dtAR = AdminRole.GetByAdminID(AdminID);
        foreach (RadListBoxItem item in roleRadListBox2.Items)
        {
            if (item.Selected)
            {
                int RoleID = Convert.ToInt32(item.Value);
                AdminRole ar = new AdminRole();
                ar.Admin_id = AdminID;
                ar.Role_id = RoleID;
                ar.Created_date = DateTime.Now;
                ar.Description = "";
                try
                {
                    AdminRole.Insert(ar);
                }
                catch (SqlException)
                {
                    resultLabel.Text += "(*) Lỗi sql khi insert<br />";
                    return;
                }
            }
        }

        resultLabel.Text = "(*) Phân quyền thành công!";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (adminRadListBox.SelectedIndex < 0)
        {
            resultLabel.Text = "(*) Chọn admin trước khi phân quyền";
            return;
        }

        int AdminID = Convert.ToInt32(adminRadListBox.SelectedValue);
        DataTable dtAR = AdminRole.GetByAdminID(AdminID);
        foreach (RadListBoxItem item in roleRadListBox2.Items)
        {
            int RoleID = Convert.ToInt32(item.Value);
            AdminRole ar = new AdminRole();
            ar.Admin_id = AdminID;
            ar.Role_id = RoleID;
            ar.Created_date = DateTime.Now;
            ar.Description = "";
            try
            {
                AdminRole.Insert(ar);
            }
            catch (SqlException)
            {
                resultLabel.Text += "(*) Lỗi sql khi insert<br />";
                return;
            }
        }

        resultLabel.Text = "(*) Phân quyền thành công!";
    }
    protected void Button4_Click(object sender, EventArgs e)
    {

    }
    protected void Button3_Click(object sender, EventArgs e)
    {

    }
}
